/* 
 * File:   Xservice.cpp
 * Author: minaev
 * 
 * Created on 2 Октябрь 2013 г., 14:31
 */

#include "Xservice.h"

#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <stdarg.h>
#include <fcntl.h>
#include <iosfwd>
#include <fstream>
#include <unistd.h>
#include <math.h>

Xservice::Xservice() {
}

Xservice::Xservice(const Xservice& orig) {
}

Xservice::~Xservice() {
}

char* Xservice::getEnv(char** env, const char* nameEnv) {
    char* res = NULL;
    int cnt_chr = 0;

    for (int i = 0; (env[i] != NULL && res == NULL); i++) {
        res = env[i];
        cnt_chr = 0;

        for (int j = 0; nameEnv[j] != 0 && env[i][j] != 0 && res != NULL; j++) {
            if (nameEnv[j] != env[i][j]) {
                res = NULL;
            }
            cnt_chr++;
        }

        if (res != NULL) {
            while (env[i][cnt_chr] == ' ' || env[i][cnt_chr] == '\t') {
                cnt_chr++;
            }
            if (env[i][cnt_chr] != '=') {
                res = NULL;
            } else {
                cnt_chr++;
            }
            while (env[i][cnt_chr] == ' ' || env[i][cnt_chr] == '\t') {
                cnt_chr++;
            }
            res = (char*) (env[i] + cnt_chr);

        }
    }
    return res;
}

int Xservice::strlen(const char* str) {
    const char* buf = str;
    while (*(str++));
    return int(str - buf - 1);
}

char* Xservice::getCurTime() {
    time_t curtime;
    struct tm struct_time;
    static char data_res[] = "0000-00-00.00-00-00";
    time(&curtime);
    struct_time = *localtime(&curtime);
    strftime(&data_res[0], 21, "%Y-%m-%d.%H-%M-%S", &struct_time);
    return &data_res[0];
}

char** bufch;

char* Xservice::catStr(int n, int nodel, ...) {
    int sumlen = 0;
    //static ;
    static char* res = NULL;
    static int ndel = 0;
    va_list strs;
    va_start(strs, n);
    bufch = new char*[n];
    int i = 0;
    while (i < n) {
        bufch[i] = va_arg(strs, char*);
        sumlen += Xservice::strlen(bufch[i]);
        i++;
    }
    va_end(strs);

    if (res != NULL && ndel == 0) {
        delete res;
    } else {
        ndel = 0;
    }

    res = new char[sumlen + 1];
    res[sumlen] = 0;

    int cur_res = 0;
    i = 0;
    while (i < n) {
        while (*(bufch[i])) {
            res[cur_res] = *(bufch[i]);
            cur_res++;
            bufch[i]++;
        }
        i++;
    }
    ndel = nodel;
    delete [] bufch;

    return res;
}

char* Xservice::toStr(char** strs) {
    int len = 0;
    int i = 0;
    int j = 0;
    for (i = 0; (strs[i] != NULL); i++) {
        len += strlen(strs[i]);
    }
    static char* res;
    if (res != NULL) {
        delete [] res;
    }
    res = new char[len + 1];
    res[len] = 0;
    len = 0;
    for (i = 0; (strs[i] != NULL); i++) {
        j = 0;
        while (strs[i][j] != 0) {
            res[len] = strs[i][j];
            j++;
            len++;
        }
    }
    return res;
}

char* Xservice::copyStr(const char* str) {
    static char* res;
    int lstr = strlen(str);
    res = new char[lstr + 1];
    res[lstr] = 0;
    while (lstr > 0) {
        lstr--;
        res[lstr] = str[lstr];
    }
    return res;
}

int Xservice::createFile(const char* filename) {
    static FILE* file = fopen(filename, "rw");

    if (file == NULL) {
        int f = open(filename, O_CREAT | O_RDWR, 0664);
        //int f = creat(filename,O_CREAT);
        close(f);
    }
    file = fopen(filename, "rw");

    if (file != NULL) {
        fclose(file);
        return 1;
    } else {
        return 0;
    }
}

int Xservice::backupFile(const char* src, const char* dst) {
    std::ifstream source(src, std::ios::binary);
    std::ofstream dest(dst, std::ios::binary);

    dest << source.rdbuf();

    source.close();
    dest.close();
}

/**
 * Integer to char*. 
 * Warning: previous return string has been cleared.
 * @param val - value
 * @return char* - string of value
 */
char* Xservice::itos(int val) {
    int bval = val;
    int lendig = 0;
    while (bval > 10) {
        bval /= 10;
        lendig++;
    }
    static char* resmsg = NULL;
    if (resmsg != NULL) {
        delete [] resmsg;
    }
    resmsg = new char[lendig + 1];
    resmsg[lendig] = 0;
    bval = 0;
    while (lendig >= 0) {
        int pow10 = 1;
        for(int i=0;i<lendig;i++) pow10*=10;
        resmsg[bval] = val / (int) pow10 % 10 + 0x30;
        lendig--;
        bval++;
    }
    return resmsg;

}

void Xservice::printArrayToHexStr(const char* arr, int len) {
    Xmsg::printf("[ ");
    for(int i=0;i<len;i++){
        Xmsg::printf("0x%x, ",(unsigned char)arr[i]);
        
    }
    Xmsg::printf("]\n");
}

int Xservice::charToHex(const char ch){
    if(ch>='0' && ch<='9'){
        return ch-'0';
    }
    if(ch>='a'&&ch<='f'){
        return ch+10-'a';
    }
    if(ch>='A'&&ch<='F'){
        return ch+10-'A';
    }
    return 0;
}